$(function(){
    var c_city=Utils.getCookie("currentCity");
    var l_city=Utils.getCurrentCity("currentCity");
    var curr_city=document.getElementById("currentCity");
    function getCurr_city(){
        var  reqUrl='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
        function callback(){
            console.log("当前城市"+remote_ip_info.city);
            var city=remote_ip_info.city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;
        }
        //如果已经定位过,直接设置为已定位的，否则重新自动定位
        if(l_city||c_city){
            console.log("a");
            var city=l_city||c_city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;    
        }else{
            console.log("b");
            $.getScript(reqUrl,callback);
        }
        setIndexData(curr_city.innerHTML);
        $(".changecity a").on("click",function(){
            location.href="./address.html?currentCity="+curr_city.innerHTML;
        });
    }
    //定位城市
    getCurr_city(setIndexData);
    
    function setIndexData(current_city){
        var address;
        if(c_city||l_city){
            address=c_city||l_city;
        }else{
            address=current_city;
        }
         //效果操作
        $(document).on("click",".city-ul li",function(){
            console.log("aaaa");
            $(this).css("color","#37c7b7");
            $(this).siblings().css("color","#141414");
            location.href="./index.html?currentCity="+$(this).html();
        }); 
        $(".searchMenu").on({
            mouseover:function(){
                $(this).find(".searchTab").addClass("show").removeClass("hide");    
            },
            mouseout:function(){
                $(this).find(".searchTab").addClass("hide").removeClass("show");
            }
        });
         //数据操作
         var fullName=$(".provice-default em").html();
         var url=Utils.reqUrl,
            asyn="true",
            type="post";
         var req_area=JSON.stringify({
                "code":"getPCDictAreaList",
                "version":"1.0",
                "data":{
                    "level":"root"
                }
         });
         var area_data=JSON.stringify({
            "code":"getDictAreaList",
            "version":"1.0",
            "data":{
                "level":"1",
                "fullName":fullName
            }
         });
         function getData(url,area_data,call_func,asyn,type){
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                contentType:"application/json",
                data:area_data,
                async:asyn,
                success:function(data){
                    call_func(data);  
                },
                error:function(msg){
                    console.log("异常："+msg);
                }   
            });        
        }
         getData(url,req_area,call_func2,asyn,type);
         getData(url,req_area,call_func3,asyn,type);
         getData(url,area_data,call_func4,asyn,type);
         function call_func2(data){
            console.log(data);
            var all_area=data.data.pda;
            var all_len=all_area.length;
            var dataStr='';

            for(var i=0;i<all_len;i++){
                var c_area=all_area[i].areas;
                var c_len=c_area.length;
                var li='';
                for(var j=0;j<c_len;j++){
                    li+='<li>'+c_area[j].areaName+'</li>';
                }
                dataStr+='<div class="more boedBottome7e7e7"><span class="addressName col36c font18 l">'+all_area[i].areaName+'</span><ul class="city-ul l">'+li+'</ul><div class="clears"></div></div>';

            }
             $(".moreAddress").html(dataStr);

             $(".city-ul li").on("click",function(){
                 currentCity=$(this).html();
                 $(".changecity span").html(currentCity);
                 Utils.setCookie("currentCity",currentCity);
             });
        }
         function call_func3(data){
             var all_areaS=data.data.pda;
             var all_lens=all_areaS.length;
             var li='';
             for(var i=0;i<all_lens;i++){
                 li+='<li class="">'+all_areaS[i].areaName+'</li>';
             }
             $(".proviceS ul").html(li);

             $(".proviceS ul li").on("click",function(){
                var curr = $(this).html(); $(this).parents(".searchTab").addClass("hide").removeClass("show").siblings(".searchSelected").find("em").html(curr);
                 var area_data=JSON.stringify({
                    "code":"getDictAreaList",
                    "version":"1.0",
                    "data":{
                        "level":"1",
                        "fullName":curr
                    }
                 });
                  getData(url,area_data,call_func4,asyn,type);
            }); 
        }
        function call_func4(data){
             console.log(data);
             var all_areaC=data.data.dictAreas;
             var all_lenc=all_areaC.length;
             var areaPname=all_areaC[0].areaName;
             var lir='';
             $(".city-default em").html(areaPname);
             for(var i=0;i<all_lenc;i++){
                 lir+='<li class="">'+all_areaC[i].areaName+'</li>';
             }
             $(".proviceC ul").html(lir);
             $(".proviceC ul li").on("click",function(){
                var curr = $(this).html();
    $(this).parents(".searchTab").addClass("hide").removeClass("show").siblings(".searchSelected").find("em").html(curr);
            }); 
        }
         //地址筛选
        var currentCity=Utils.getCookie("currentCity");
            $(".changecity span").html(currentCity);
            $(".choose").on("click",function(){
                currentCity=$(".city-default em").html();
                $(".changecity span").html(currentCity);
                Utils.setCookie("currentCity",currentCity);
                location.href="./index.html?currentCity="+currentCity;
        });
    }
 });